﻿using System;
using System.ComponentModel;
using System.Threading.Tasks;
using Avalonia;
using Avalonia.Controls;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;

namespace Launcher;

public partial class SettingModel:ObservableObject
{
    [ObservableProperty] 
    private string? _cachePath;
    [ObservableProperty]
    private bool _useMultiThreadedDownloading;
    
    private readonly SettingWindow _settingWindow;
    public SettingModel(SettingWindow settingWindow)
    {
        _settingWindow = settingWindow;
        CachePath = Setting.Instance.CachePath;
        UseMultiThreadedDownloading = Setting.Instance.UseMultiThreadedDownloading;
    }
    
    [RelayCommand]
    private async Task SelectCachePath()
    {
        var dialog = new OpenFolderDialog();
        var result = await dialog.ShowAsync(_settingWindow);

        // 用户选择了文件夹后执行的代码
        if (!string.IsNullOrWhiteSpace(result))
        {
            // 处理选择的文件夹路径
            Console.WriteLine($"Selected folder: {result}");
            CachePath = result;
            Setting.Instance.CachePath = result;
            Setting.Save();
        }
    }

    protected override void OnPropertyChanged(PropertyChangedEventArgs e)
    {
        base.OnPropertyChanged(e);
        if (e.PropertyName == nameof(UseMultiThreadedDownloading))
        {
            Setting.Instance.UseMultiThreadedDownloading = UseMultiThreadedDownloading;
            Setting.Save();
        }
    }
}